import { RequestUtil } from '@/utils/request.js';

const getList = ({ pageNo, pageSize, searchQuery, sortBy, isAsc }, requestId) => {
  return RequestUtil.get('/resource', {
    requestId: requestId,
    params: {
      pageNo: pageNo,
      pageSize: pageSize,
      sortBy: sortBy,
      isAsc: isAsc,
      ...searchQuery
    }
  });
};

const upload = (file, pId, path) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('pId', pId ?? 0);
  formData.append('path', path ?? '');
  return RequestUtil.post('/upload', {
    body: formData,
    timeout: 120 * 1000
  });
};

const deleteByIds = (ids) => {
  return RequestUtil.delete('/resource?ids=' + (Array.isArray(ids) ? ids.join(',') : ''));
};

const getBreadcrumb = (id) => {
  return RequestUtil.get('/resource/breadcrumb/' + id);
};

const createDir = (name, pId) => {
  return RequestUtil.post('/createDir', {
    data: {
      directoryName: name,
      parentId: pId
    }
  });
};

const update = (id, resource) => {
  return RequestUtil.put('/resource', {
    data: {
      resourceId: id,
      ...resource
    }
  });
};

export const ResourceApi = {
  getList,
  upload,
  deleteByIds,
  getBreadcrumb,
  createDir,
  update
};
